/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "IAudio.h"
#include "core/core.h"
#include "Audio_OpenAL.h"

audio::IAudio *g_pAudio = NULL;

audio::IAudio* coreGetAudio()
{
	return g_pAudio;
}

bool coreInitAudio(bool b3D) {	
	if (!g_pAudio) {

		g_pAudio = new openAL::Audio_OpenAL();
		
		if (!g_pAudio->init(b3D)) {
			coreGetLog()->WriteError("Initialization of Audio device failed. ", __FILE__, __LINE__);
			return false;
		}
	}
	return true;
}

bool coreDestroyAudio() {	
	if (g_pAudio) {
		g_pAudio->Destroy();
		g_pAudio = NULL;
		return true;
	}		
	return false;
}


namespace audio {

IAudio::IAudio(void):
	m_fSfxVolume(1.0f),
	m_fMusicVolume(1.0f),
	m_fSpeechVolume(1.0f),
	m_bSFXMuted(false)
{
	// if there already are prefered sfx, music, and speech volumes in the config file -> use them
	// otherwise initialize them with 1.0 and store them in the config file:
	std::stringstream str;
	if (!coreGetConfig()->isDefined("sfx_volume"))
	{
		str << m_fSfxVolume;
		coreGetConfig()->setString("sfx_volume" , str.str());
	}
	else
	{
		const std::string vol = coreGetConfig()->getString("sfx_volume");
		m_fSfxVolume = (float)atof(vol.c_str());
	}
	str.str("");
	str.clear();

	if (!coreGetConfig()->isDefined("music_volume"))
	{
		str << m_fMusicVolume;
		coreGetConfig()->setString("music_volume" , str.str());
	}
	else
	{
		const std::string vol = coreGetConfig()->getString("music_volume");
		m_fMusicVolume = (float)atof(vol.c_str());
	}
	str.str("");
	str.clear();

	if (!coreGetConfig()->isDefined("speech_volume"))
	{
		str << m_fSpeechVolume;
		coreGetConfig()->setString("speech_volume" , str.str());
	}
	else
	{
		const std::string vol = coreGetConfig()->getString("speech_volume");
		m_fSpeechVolume = (float)atof(vol.c_str());
	}
	str.str("");
	str.clear();
}

IAudio::~IAudio(void)
{
}

float IAudio::getSfxVolume(bool _ignoreMute) {
	if (_ignoreMute) 
		return m_fSfxVolume;

	if (m_bSFXMuted) {
		return 0;
	}

	return m_fSfxVolume;
}

void IAudio::setSfxVolume(float vol)
{
	m_fSfxVolume = vol;

	// updating the config file:
	std::stringstream str;
	str << m_fSfxVolume;
	coreGetConfig()->setString("sfx_volume" , str.str());
	str.str("");
	str.clear();
}

float IAudio::getMusicVolume()
{
	return m_fMusicVolume;
}

void IAudio::setMusicVolume(float vol)
{
	m_fMusicVolume = vol;

	// updating the config file:
	std::stringstream str;
	str << m_fMusicVolume;
	coreGetConfig()->setString("music_volume" , str.str());
	str.str("");
	str.clear();
}

float IAudio::getSpeechVolume()
{
	return m_fSpeechVolume;
}

void IAudio::setSpeechVolume(float vol)
{
	m_fSpeechVolume = vol;

	// updating the config file:
	std::stringstream str;
	str << m_fSpeechVolume;
	coreGetConfig()->setString("speech_volume" , str.str());
	str.str("");
	str.clear();
}

void IAudio::destroyAllBuffers() {
	
	std::map<std::string, ISoundBuffer*>::iterator it;
	
	it = m_mISoundBuffer.begin();

	while (it != m_mISoundBuffer.end()) {
		DELETE_POINTER(it->second);
		it = m_mISoundBuffer.erase(it);
	}	
}

void IAudio::muteSFX(bool _val) {
	m_bSFXMuted = _val;
}



} // namespace
